/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ------------------
 * PieChartDemo1.java
 * ------------------
 * (C) Copyright 2002-2004, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: PieChart.java 34 2009-09-01 06:02:32Z maven.yu $
 *
 * Changes
 * -------
 * 18-Apr-2002 : Version 1 (DG);
 * 25-Jun-2002 : Removed unnecessary import (DG);
 * 28-Aug-2002 : Centered frame on screen (DG);
 * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
 * 10-Mar-2004 : Updated for rewritten pie chart classes (DG);
 *
 */

package com.kingre.et.chart;

import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Map;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

/**
 * A simple demonstration application showing how to create a pie chart using data from a
 * {@link DefaultPieDataset}.
 *
 * @author David Gilbert
 */
public class PieChart{

    public static JFreeChart generateChart(List<Map<String, String>> list,
            String[] levelName, String titleName){
        PieDataset dataset = createDataset(list, levelName);   
        JFreeChart chart = createChart(dataset, titleName);   
        return chart;
    }
    
    /**
     * Creates a sample dataset.
     * 
     * @return a sample dataset.
     */
    private static PieDataset createDataset(List<Map<String, String>> list, String[] levelName) {
        DefaultPieDataset dataset = new DefaultPieDataset();
        for(int i = 0; i < list.size(); i++){
            dataset.setValue(levelName[i], Integer.parseInt(list.get(i).get("number")));
        }
        return dataset;        
    }
    
    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    * 
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************
    
    /**
     * Creates a chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return a chart.
     */
    private static JFreeChart createChart(PieDataset dataset, String titleName) {
        
        JFreeChart chart = ChartFactory.createPieChart(
            titleName,          // chart title
            dataset,            // data
            true,               // include legend
            true,
            false
        );
        
        chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 16));
        chart.setBackgroundPaint(Color.WHITE);
        LegendTitle legend = chart.getLegend(0);
        legend.setItemFont(new Font("黑体", 0, 11));
        PiePlot pieplot = (PiePlot) chart.getPlot();
        pieplot.setLabelFont(new Font("黑体", 0, 11));
        pieplot.setCircular(true);
        pieplot.setLabelGap(0.02D);
        pieplot.setBackgroundPaint(Color.LIGHT_GRAY);
                pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
                "{0} {2}", NumberFormat.getNumberInstance(), new DecimalFormat("0.0%")));
               
//        pieplot.setLegendLabelGenerator(new StandardPieItemLabelGenerator("{0} {2}")); 
//        LegendTitle legendtitle = new LegendTitle(chart.getPlot());
//        BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
//        blockcontainer.setBorder(new BlockBorder(1.0D, 1.0D, 1.0D, 1.0D));
//        LabelBlock labelblock = new LabelBlock("本周商品出货单:", new Font("宋体", 1, 12));
//        labelblock.setPadding(5D, 5D, 5D, 5D);
//        blockcontainer.add(labelblock, RectangleEdge.TOP);
//        LabelBlock labelblock1 = new LabelBlock("by 赵永亮 2008.6.24");
//        labelblock1.setPadding(8D, 20D, 2D, 5D);
//        blockcontainer.add(labelblock1, RectangleEdge.BOTTOM);
//        BlockContainer blockcontainer1 = legendtitle.getItemContainer();
//        blockcontainer1.setPadding(2D, 10D, 5D, 2D);
//        blockcontainer.add(blockcontainer1);
//        legendtitle.setWrapper(blockcontainer);
//        legendtitle.setPosition(RectangleEdge.RIGHT);
//        legendtitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
//        chart.addSubtitle(legendtitle);
        return chart;
        
    }
    

}
